<template>
  <div>
    <!-- 查询 -->
    <div style="height: 25px;line-height: 50px;margin: 15px 0; display: flex;
            justify-content: center;
            align-items: center;">
            <div style="height: 50px;line-height: 50px;margin-right: 10px">
      <el-button type="primary" @click="add">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
    </div>
      <el-input
        style="width: 200px;"
        clearable
        placeholder="请输入机房名"
        suffix-icon="el-icon-search"
        type="text"
        v-model="search">
      </el-input>
      <div class="block">
        <el-date-picker
          v-model="searchdate"
          suffix-icon="el-icon-search"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择巡检日期">
        </el-date-picker>
        <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      </div>
       <!-- 新增 -->
    
    </div>
   
    <!-- 机房巡检记录展示 -->
    <el-table
      :data="tableData"
      height="500"
      stripe
      v-loading="loading"
      style="width: 100%">
      <el-table-column prop="id" label="id" width="100">
      </el-table-column>
      <el-table-column prop="rid" label="机房" width="100">
      </el-table-column>
      <el-table-column prop="humidity" label="湿度" width="100">
      </el-table-column>
      <el-table-column prop="tp" label="温度" width="100">
      </el-table-column>
      <el-table-column prop="light" label="照明" width="100">
      </el-table-column>
      <el-table-column prop="noise" label="噪声" width="100">
      </el-table-column>
      <el-table-column prop="remarks" label="机房巡检情况">
      </el-table-column>
      <el-table-column
        prop="patroldate"
        label="巡检日期"
      >
      </el-table-column>
      <el-table-column label="操作">
      <template slot-scope="scope">
        <el-button type="primary" icon="el-icon-edit" circle @click="handleEdit(scope.row)"/>
        <el-popconfirm title="确定删除记录吗" @confirm="handleDelete(scope.row.id)">
          <template #reference>
            <el-button type="danger" icon="el-icon-delete" circle />
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
    </el-table>

    <div style= "padding: 10px 0 ; display:  flex;
            justify-content: center;
            align-items: center;">
      <el-pagination
        style=" position: absolute;margin: auto"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 16, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next"
        :pager-count="7"
        :total="total">
      </el-pagination>
    </div>

    <!-- 新增修改巡检信息对话框&表单 -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="50%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item  prop="button">
        <el-button  v-model="ruleForm.button" disabled>{{ this.ruleForm.button }}</el-button>
      </el-form-item>
      <el-form-item label="机房" prop="rid">
        <el-select v-model="ruleForm.rid" placeholder="请选择">
          <el-option
            v-for="item in this.options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>

      </el-form-item>
      <el-form-item label="湿度" prop="humidity">
        <el-input  v-model.number="ruleForm.humidity"></el-input>
      </el-form-item>
      <el-form-item label="温度(℃)" prop="tp">
        <el-input  v-model.number="ruleForm.tp"></el-input>
      </el-form-item>
      <el-form-item label="噪声" prop="noise">
        <el-input  v-model.number="ruleForm.noise"></el-input>
      </el-form-item>
      <el-form-item label="照明" prop="light">
        <el-radio-group v-model="ruleForm.light">
          <el-radio label="明亮" class="el-icon-sunny"></el-radio>
          <el-radio label="暗沉" class="el-icon-s-opportunity"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input type="textarea" v-model="ruleForm.remarks"></el-input>
      </el-form-item>
      <el-form-item label="巡检日期" required>
        <el-col :span="16">
          <el-form-item prop="patroldate">
            <el-date-picker  value-format="yyyy-MM-dd" placeholder="选择日期" v-model="ruleForm.patroldate" style="width: 100%;"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>

</template>

<script>
import request from "../../utils/request";

export default {
  name: 'Patrol',

  data() {
    return {
      search: '',
      searchdate: '',
      dialogVisible: false,
      tableData: [],
      currentPage: 1,
      pageSize: 16,
      total: 0,
      ruleForm:{},
      options: [{
        value: 'A1-201',
        label: 'A1-201'
      },{
        value: 'A1-202',
        label: 'A1-202'
      },{
        value: 'A1-203',
        label: 'A1-203'
      },{
        value: 'A1-204',
        label: 'A1-204'
      },
        {
          value: 'A1-301',
          label: 'A1-301'
        },{
          value: 'A1-302',
          label: 'A1-302'
        },{
          value: 'A1-303',
          label: 'A1-303'
        },{
          value: 'A1-304',
          label: 'A1-304'
        },
        {
          value: 'A1-401',
          label: 'A1-401'
        },{
          value: 'A1-402',
          label: 'A1-402'
        },{
          value: 'A1-403',
          label: 'A1-403'
        },{
          value: 'A1-404',
          label: 'A1-404'
        },
        {
          value: 'A1-501',
          label: 'A1-501'
        },{
          value: 'A1-502',
          label: 'A1-502'
        },{
          value: 'A1-503',
          label: 'A1-503'
        },{
          value: 'A1-504',
          label: 'A1-504'}],
      rules: {
        rid: [
            { required: true, message: '请填写巡检机房', trigger: 'change' }
          ],
          humidity: [
            { required: true, message: '请填写机房湿度', trigger: 'change' }
          ],
          tp: [
            { required: true, message: '请填写机房温度', trigger: 'change' }
          ],
          noise: [
            { required: true, message: '请填写机房噪声', trigger: 'change' }
          ],
          remarks: [
            { required: true, message: '请填写机房巡检情况', trigger: 'change' }
          ],
          light: [
            { required: true, message: '请选择机房照明情况', trigger: 'change' }
          ],
          patroldate: [
            { required: true, message: '请选择巡检日期', trigger: 'change' }
          ],
      },
      loading: true
    }
  },
  created() {
    this.load()
  },
  methods:{
    tableRowClassName({row, rowIndex}) {
        if (rowIndex === 1) {
          return 'warning-row';
        } else if (rowIndex === 3) {
          return 'success-row';
        }
        return '';
      },
    //加载时展示巡检记录
    load(){
      request.get("/sys/eroom/selectAll",{
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search,
          searchdate: this.searchdate
        }
      }).then(res => {
        console.log(res)
        console.log(this.search)
        console.log(this.searchdate)
        this.tableData = res.data.records
        this.loading = false
        console.log(this.tableData)
        this.total = res.data.total
      })
    },
    // 新增机房巡检记录
    add(){
      this.ruleForm = {}
          this.ruleForm.button = "新增机房巡检记录",
         this.dialogVisible = true;

      },
      // 表单提交触发 修改记录，新增记录
      submitForm(formName) { 
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.ruleForm.button == "修改信息"){ 
          request.put("/sys/eroom/update",this.ruleForm).then(res =>{
            //console.log(res);
            // console.log(this.ruleForm)
            if (res.code === '1'){
              this.$message({
                type: "success",
                message: "修改信息成功"
              })
            } else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
            this.load() //刷新
            this.dialogVisible = false
          });
          this.load() //刷新
          this.dialogVisible = false
        }else { //新增
          request.post("/sys/eroom/add",this.ruleForm).then(res =>{
            console.log(res);
            console.log(this.ruleForm)
            if (res.code === '1'){
              this.$message({
                type: "success",
                message: "巡检记录成功"
              })
            } else {
              this.$message({
                type: "error",
                message: res.msg
              })
            }
            this.load() //刷新
            this.dialogVisible = false
          });
        }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
        
      },
      //重置新增 表单内容
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      //表格编辑功能
      handleEdit(row){
        this.ruleForm = JSON.parse(JSON.stringify(row)) //与之前对象隔离开,避免浅拷贝问题
        this.ruleForm.button = "修改信息",
        this.dialogVisible = true;
      },
      //表格删除功能
      handleDelete(id){
        request.delete("/sys/eroom/delete/"+id).then(res=>{
          if (res.code === '1'){
            this.$message({
              type: "success",
              message: "删除成功"
            })
          } else {
            this.$message({
              type: "error",
              message: res.msg
            })
          }
          this.load()  //删除后重新加载
        })
      },
    handleSizeChange(pageSize) {  //改变当前每页显示个数触发
      this.pageSize = pageSize;
      this.load()
    },
    handleCurrentChange(currentPage) {  //改变当前页码 触发
      this.currentPage = currentPage;
      this.load()
    }
  }
}
</script>

<style>

</style>
